<template>
  <div>
    <el-upload
      class="avatar-uploader"
      :action="`${VUE_APP_BASE_API}/v1/index/upload`"
      name="file"
      :show-file-list="false"
      :on-success="uploadSuccess"
      ref="uplocd"
      id="quill-upload"
      multiple
    >
    </el-upload>
    <el-upload
      class="avatar-uploader2"
      :action="`${VUE_APP_BASE_API}/v1/index/upload`"
      name="file"
      :show-file-list="false"
      :on-success="uploadSuccess2"
      ref="uplocd"
      id="quill-upload2"
      multiple
    >
    </el-upload>
    <quill-editor
      class="editor"
      v-model="desc"
      ref="editors22"
      :options="editorOption"
      @change="onEditorChange()"
    >
    </quill-editor>
  </div>
</template>

<script>
const toolbarOptions = [
  ["bold", "italic", "underline", "strike"], // toggled buttons
  ["blockquote", "code-block"],
  [
    {
      header: 1,
    },
    {
      header: 2,
    },
  ], // custom button values
  [
    {
      list: "ordered",
    },
    {
      list: "bullet",
    },
  ],
  [
    {
      script: "sub",
    },
    {
      script: "super",
    },
  ], // superscript/subscript
  [
    {
      indent: "-1",
    },
    {
      indent: "+1",
    },
  ], // outdent/indent
  [
    {
      direction: "rtl",
    },
  ], // text direction

  [
    {
      size: ["small", false, "large", "huge"],
    },
  ], // custom dropdown
  [
    {
      header: [1, 2, 3, 4, 5, 6, false],
    },
  ],

  [
    {
      color: [],
    },
    {
      background: [],
    },
  ], // dropdown with defaults from theme
  [
    {
      font: [],
    },
  ],
  [
    {
      align: [],
    },
  ],
  ["image", "video"],
  ["clean"], // remove formatting button
];
import { quillEditor } from "vue-quill-editor";
import { VUE_APP_BASE_API } from "@/config";
export default {
  props: ["clearDesc", "contentDesc"],
  data() {
    return {
      VUE_APP_BASE_API,
      data: "",
      data2: "",
      quillUpdateImg: false,
      desc: "",
      editorOption: {
        placeholder: "请输入正文",
        theme: "snow", // or 'bubble'
        modules: {
          toolbar: {
            container: toolbarOptions, // 工具栏
            handlers: {
              image: function (value) {
                if (value) {
                  document.querySelector("#quill-upload input").click();
                } else {
                  this.quill.format("image", false);
                }
              },
              video: function (value) {
                if (value) {
                  document.querySelector("#quill-upload2 input").click();
                } else {
                  this.quill.format("video", false);
                }
              },
              upload: function (value) {
                if (value) {
                  document.querySelector("#quill-upload3 input").click();
                } else {
                  this.quill.format("upload", false);
                }
              },
              // 'link': function(value) {
              //   if (value) {
              //     document.querySelector('#quill-upload input').click()
              //   } else {
              //     this.quill.format('link', false);
              //   }
              // },
            },
          },
        },
      },
    };
  },
  watch: {
    clearDesc: {
      handler(o, i) {
        if (o == 1) {
          this.desc = "";
        }
      },
      immediate: true,
      deep: true,
    },
    contentDesc: {
      handler(newName, oldName) {
        this.desc = newName;
      },
      immediate: true,
      deep: true,
    },
  },
  mounted() {},
  methods: {
    // 上传图片成功
    uploadSuccess(res, file) {
      // 获取富文本组件实例
       let quill = this.$refs.editors22.quill;
      // 如果上传成功
        // console.log(this.$refs.editors22.quill,999);
      this.data = res.data.url;
      if (res.code == 1 && res.data !== null) {
        // 获取光标所在位置
        // selection.savedRange.index
        let length = quill.getSelection().index;
        // 插入图片 res.info为服务器返回的图片地址
        quill.insertEmbed(length, "image", this.data);
        // 调整光标到最后
        quill.setSelection(length + 1);
      } else {
        this.$message.error("图片插入失败");
      }
      // loading动画消失
      this.quillUpdateImg = false;
    },

    // 上传视频成功
    uploadSuccess2(res, file) {
      // 获取富文本组件实例
      let quill = this.$refs.editors22.quill;
      // 如果上传成功
      this.data2 = res.data.url;
      if (res.code == 1 && res.data !== null) {
        // 获取光标所在位置
        let length = quill.getSelection().index;
        // 插入图片 res.info为服务器返回的图片地址
        quill.insertEmbed(length, "video", this.data2);
        // 调整光标到最后
        quill.setSelection(length + 1);
      } else {
        this.$message.error("图片插入失败");
      }
      // loading动画消失
      this.quillUpdateImg = false;
    },

    onEditorChange() {
      //内容改变事件
      this.$emit("descVal", this.desc);
    },
    clearFiles() {
      this.$refs["uplocd"].clearFiles();
    },
  },
};
</script>

<style>
.edit_container,
.quill-editor {
  height: 300px;
}

.ql-snow .ql-picker.ql-size .ql-picker-label::before,
.ql-snow .ql-picker.ql-size .ql-picker-item::before {
  content: "14px";
}

.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="small"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="small"]::before {
  content: "10px";
}

.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="large"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="large"]::before {
  content: "18px";
}

.ql-snow .ql-picker.ql-size .ql-picker-label[data-value="huge"]::before,
.ql-snow .ql-picker.ql-size .ql-picker-item[data-value="huge"]::before {
  content: "32px";
}

.ql-snow .ql-picker.ql-header .ql-picker-label::before,
.ql-snow .ql-picker.ql-header .ql-picker-item::before {
  content: "文本";
}

.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="1"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="1"]::before {
  content: "标题1";
}

.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="2"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="2"]::before {
  content: "标题2";
}

.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="3"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="3"]::before {
  content: "标题3";
}

.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="4"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="4"]::before {
  content: "标题4";
}

.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="5"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="5"]::before {
  content: "标题5";
}

.ql-snow .ql-picker.ql-header .ql-picker-label[data-value="6"]::before,
.ql-snow .ql-picker.ql-header .ql-picker-item[data-value="6"]::before {
  content: "标题6";
}

.ql-snow .ql-picker.ql-font .ql-picker-label::before,
.ql-snow .ql-picker.ql-font .ql-picker-item::before {
  content: "标准字体";
}

.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="serif"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="serif"]::before {
  content: "衬线字体";
}

.ql-snow .ql-picker.ql-font .ql-picker-label[data-value="monospace"]::before,
.ql-snow .ql-picker.ql-font .ql-picker-item[data-value="monospace"]::before {
  content: "等宽字体";
}
</style>
